// pages/memberDetail/memberDetail.js
var utils = require('../../../../utils/util.js') 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:[],
    date:'',
    isEmpty: false,
    vipId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.vipId = options.id;
    this.getMember(this.data.vipId)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  //下拉刷新
  onPullDownRefresh: function () {
    this.setData({
      data: [],
    });
    this.getMember(this.data.vipId)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  getMember: function (id) {
    var that = this;
    var userinfo = wx.getStorageSync("userinfo");
    var token = userinfo.wap_token;
    var data = {
      url: "/v1/userbill/mySonBill",
      token: token,
      type: 0,
      user_id:id,
      start_time: that.data.date,
      end_time: that.data.date,
      page: 1,
      limit: 100,
    };
    utils.request(data).then(function (res) {
      let now = new Date().getFullYear();
      let k = res.data.length;

      var isEmpty = k==0 ? true : false;
      that.setData({
        isEmpty: isEmpty
      });
      for (now; now > 2016; now--) {
        let y = 0;
        let year = new Array();
        for (let j = 0; j < k; j++) {
          let time = res.data[j];
          var pastY = res.data[j].y;
          if (pastY == now) {
            year[y++] = time
          }
        }
        let s = year.length;
        for (let m = 12; m > 0; m--) {
          let month = new Array();
          let Sum = 0;
          let num = 0;
          for (let j = 0; j < s; j++) {
            let time2 = year[j];
            let pastM = year[j].m;
            if (m == pastM) {
              month[num++] = time2;
              Sum += parseFloat(year[j].award_fanyong) + parseFloat(year[j].award_fenyong);
              var a = pastM;
            }
          }
          if (month != '') {
            month.unshift({ year: now, month: a, Sum: Sum });
            that.data.data.push.apply(that.data.data, month);
          }
        }
      };
      that.setData({
        data: that.data.data
      })
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value,
      data:[]
    })
    this.getMember(this.data.vipId)
  },
})